/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nxlv2.nomad.view.ficheClient;

import com.nxlv2.nomad.accessdb.services.ClientServices;
import com.nxlv2.nomad.accessdb.services.ContactServices;
import com.nxlv2.nomad.businessobjects.Client;
import com.nxlv2.nomad.businessobjects.Contact;
import java.awt.Color;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author Nastasia
 */
public class FormulaireContact extends javax.swing.JFrame {

    private final FicheClient fc;
    private final Client cl;

    /**
     * Creates new form FormulaireContact
     */
    public FormulaireContact(FicheClient fc, Client cl) {
        initComponents();
        this.fc = fc;
        this.cl = cl;
        this.setLocationRelativeTo(null);
        lblError.setText("");
        lblNomClient.setText(cl.getNom());
        if(null!=fc.getContact()){
//            System.out.println("null");
            txtNom.setText(fc.getContact().getNom());
            txtPrenom.setText(fc.getContact().getPrenom());
            txtMail.setText(fc.getContact().getMail());
            txtTel.setText(fc.getContact().getTel());
            if(fc.getContact().isDefaut()){
                chekBoxPrincipale.setSelected(true);
            }
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        lblNomClient = new javax.swing.JLabel();
        lblNom = new javax.swing.JLabel();
        txtNom = new javax.swing.JTextField();
        lblPrenom = new javax.swing.JLabel();
        txtPrenom = new javax.swing.JTextField();
        lblMail = new javax.swing.JLabel();
        txtMail = new javax.swing.JTextField();
        lblTel = new javax.swing.JLabel();
        txtTel = new javax.swing.JTextField();
        chekBoxPrincipale = new javax.swing.JCheckBox();
        btnValider = new javax.swing.JButton();
        lblError = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Formulaire de contact");

        lblNomClient.setText("jLabel1");

        lblNom.setText("Nom* :");

        lblPrenom.setText("Prénom* :");

        lblMail.setText("Adresse mail* :");

        lblTel.setText("Téléphone* :");

        chekBoxPrincipale.setText("Contact principal");

        btnValider.setText("Valider");
        btnValider.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnValiderActionPerformed(evt);
            }
        });

        lblError.setForeground(new java.awt.Color(255, 0, 0));
        lblError.setText("jLabel10");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(52, 52, 52)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(chekBoxPrincipale)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(btnValider)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(lblTel)
                                .addComponent(lblMail)
                                .addComponent(lblPrenom)
                                .addComponent(lblNom))
                            .addGap(18, 18, 18)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(lblNomClient, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(txtPrenom, javax.swing.GroupLayout.DEFAULT_SIZE, 135, Short.MAX_VALUE)
                                    .addComponent(txtNom)
                                    .addComponent(txtTel, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(txtMail, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblError, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblNomClient)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblNom)
                    .addComponent(txtNom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblPrenom)
                    .addComponent(txtPrenom, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblMail)
                    .addComponent(txtMail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblTel)
                    .addComponent(txtTel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(chekBoxPrincipale)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblError)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(btnValider)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnValiderActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnValiderActionPerformed
        // TODO add your handling code here:
        ClientServices srvCl = new ClientServices();

        Pattern p = Pattern.compile("^[a-zA-Z0-9 \\-@]+$");
        Pattern p1 = Pattern.compile("^[a-zA-Z0-9\\.]+[@]{1}[a-zA-Z0-9\\.]+[\\.]{1}[a-zA-Z]+$");
        Pattern p2 = Pattern.compile("^[0-9 \\-\\+\\.]+$");
        Pattern p3 = Pattern.compile("^[a-zA-Zéèùàûüêâ \\-]+$");

        Matcher mNomCon = p3.matcher(txtNom.getText());
        Matcher mPrenCon = p3.matcher(txtPrenom.getText());
        Matcher mMailCon = p1.matcher(txtMail.getText());
        Matcher mTelCon = p2.matcher(txtTel.getText());

        int error = 0;
        // Nom Contact 
        if (!mNomCon.matches() || "".equals(txtNom.getText())) {
            lblNom.setForeground(Color.red);
            error = 1;
        } else {
            lblNom.setForeground(Color.black);
        }
        // Prénom Contact
        if (!mPrenCon.matches() || "".equals(txtPrenom.getText())) {
            lblPrenom.setForeground(Color.red);
            error = 1;
        } else {
            lblPrenom.setForeground(Color.black);
        }
        // Mail Contact
        if (!mMailCon.matches() || "".equals(txtMail.getText())) {
            lblMail.setForeground(Color.red);
            error = 1;
        } else {
            lblMail.setForeground(Color.black);
        }
        // Tel Contact
        if (!mTelCon.matches() || "".equals(txtTel.getText())) {
            lblTel.setForeground(Color.red);
            error = 1;
        } else {
            lblTel.setForeground(Color.black);
        }

        if (error == 1) {
            lblError.setText("Veuillez corriger ou compléter les champs indiqués");
        } else {
            ContactServices srvCon = new ContactServices();
            Contact cont = new Contact();
            if(null!=fc.getContact()){
                cont = fc.getContact();
            }
            cont.setNom(txtNom.getText());
            cont.setPrenom(txtPrenom.getText());
            cont.setMail(txtMail.getText());
            cont.setTel(txtTel.getText());
            if (chekBoxPrincipale.isSelected() == true) {
                System.out.println("cochée");
                List<Contact> contacts;
                contacts = srvCl.getContacts(cl);
                for (Contact contact : contacts) {
                    if (contact.isDefaut() == true) {
                        contact.setDefaut(false);
                        srvCon.update(contact);
                    }
                }
                cont.setDefaut(true);
            }
            if(null!=fc.getContact()){
                srvCon.update(cont);
            } else{
                srvCon.create(cont, cl);
            }
            fc.majContacts();
            this.dispose();
        }
    }//GEN-LAST:event_btnValiderActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnValider;
    private javax.swing.JCheckBox chekBoxPrincipale;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblError;
    private javax.swing.JLabel lblMail;
    private javax.swing.JLabel lblNom;
    private javax.swing.JLabel lblNomClient;
    private javax.swing.JLabel lblPrenom;
    private javax.swing.JLabel lblTel;
    private javax.swing.JTextField txtMail;
    private javax.swing.JTextField txtNom;
    private javax.swing.JTextField txtPrenom;
    private javax.swing.JTextField txtTel;
    // End of variables declaration//GEN-END:variables
}
